package com.ms.p2p.controller;


import com.ms.common.mapper.JsonMapper;
import com.ms.p2p.entity.RetMap;
import com.ms.p2p.plugin.IPlugin;
import com.ms.p2p.Constants;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.plugin.payment.IPaymentEvent;
import com.ms.p2p.plugin.payment.PaymentPluginBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.support.HandlerMethodInvocationException;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by siren on 2017/5/28.
 */
@Controller
@RequestMapping({ "/onlinereturn" })
public class OnlineReturnController extends BaseController {

    @Autowired
    PaymentPluginBundle paymentPluginBundle;
    @RequestMapping(value = { "/{payType}" })
    @ResponseBody
    public  String payType(HttpServletRequest request,@PathVariable String payType) throws Exception {
        List<IPlugin> plugins = paymentPluginBundle.getPluginList();
        //每个支付都有自己的定时
        for (IPlugin plugin : plugins) {
            if (payType.equals(plugin.getName())) {
              if(payType.equals("fengfu")) {
                  IPaymentEvent pay = (IPaymentEvent) plugin;
                  String msg = pay.onReturn(request);
                  if (msg.equals(Constants.HASPAY) || msg.equals(Constants.SUCCESS)) {
                      return "success";
                  }

              }
             else if(payType.equals("fuyou")) {
                  IPaymentEvent pay = (IPaymentEvent) plugin;
                  String msg = pay.onReturn(request);
              }
              else  if(payType.equals("fuyouwap")) {
                    IPaymentEvent pay = (IPaymentEvent) plugin;
                    String msg = pay.onReturn(request);
              }
              else  if(payType.equals("fuyoupcrz")) {
                  IPaymentEvent pay = (IPaymentEvent) plugin;
                  String msg = pay.onReturn(request);
              }
              else  if(payType.equals("fuyoumobile")) {
                  IPaymentEvent pay = (IPaymentEvent) plugin;
                  String msg = pay.onReturn(request);
                    if(msg.equals(Constants.SUCCESS)){
                        return JsonMapper.toJsonString(RetMap.Success("支付成功"));
                    }else{
                        return JsonMapper.toJsonString(RetMap.Error("支付失败"));
                    }
              }


            }
        }
      return null;
    }

}





